package com.bcu.calculus.service.serviceImpl;

import com.bcu.calculus.entity.*;
import com.bcu.calculus.mapper.*;
import com.bcu.calculus.service.SendService;
import com.bcu.calculus.service.UserInfoService;
import com.bcu.calculus.utils.SendUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SendServiceImpl implements SendService {
    private Logger logger= LoggerFactory.getLogger(getClass());

    @Resource
    private SendMapper sendMapper;

    @Resource
    private FlistMapper flistMapper;
    @Resource
    private SlistMapper slistMapper;
    @Resource
    private TlistMapper tlistMapper;

    @Resource
    private UserInfoService userInfoService;
    @Resource
    private ReviewMapper reviewMapper;



    @Override
    public Message<?> insert(Send send) {
        Send send1 = sendMapper.getSendByManyListAndSemseterIdAndUserId(send.getSendFlistId(),send.getSendSlistId(),send.getSendTlistId(),send.getSendSemseterId(),send.getSendUserId());

        if (send1==null){
            String sendId = "SE" + SendUtil.genSendId();
            send.setSendId(sendId);
            send.setSendCreateTime(new Date());
            send.setSendUpdateTime(new Date());
            logger.info("提交-----增加了新的提交,sendId为:"+send.getSendId());
            return sendMapper.insertSelective(send) > 0 ? Message.success("增加提交 "+send.getSendId()+" 成功") : Message.fail("增加提交 "+send.getSendId()+" 失败");
        }else {
            return Message.success("该send已经存在请根据主键修改"+send1.getSendId());
        }

    }



    @Override
    public Message<?> deleteByPrimaryKey(String sendId) {
        Send s = sendMapper.selectByPrimaryKey(sendId);
        if(s == null){
            logger.info("提交-----尝试通过关键字删除提交,但是失败了,sendId不存在,sendId为 "+sendId);
            return Message.fail("您要删除的提交不存在");
        }
        else{
            logger.info("提交-----通过关键字删除了一条数据,sendId为 "+sendId);
            return sendMapper.deleteByPrimaryKey(sendId) > 0 ? Message.success("删除提交 "+sendId+" 成功") : Message.fail("删除提交 "+sendId+" 失败");
        }
    }


    @Override
    public Message<?> updateByPrimaryKey(Send send) {
        Send s =sendMapper.selectByPrimaryKey(send.getSendId());
        if (s == null){
            return Message.fail("您要修改的提交不存在");
        }
        else {
            logger.info("提交-----通过关键字修改了一条数据,sendId为 "+send.getSendId());
            send.setSendUpdateTime(new Date());
            return sendMapper.updateByPrimaryKeySelective(send) > 0 ? Message.success("更新提交 "+send.getSendId()+" 成功") : Message.fail("更新提交 "+send.getSendId()+" 失败");
        }
    }



    @Override
    public Message<Send> selectByPrimaryKey(String sendId) {
        logger.info("提交-----通过关键字查询,sendId为 "+sendId);
        return Message.success(null).add(sendMapper.selectByPrimaryKey(sendId));
    }



    @Override
    public Message<List<Send>> selectAllSend() {
        logger.info("提交-----查询所有提交");
        return Message.success(null).add(sendMapper.selectAllSend());
    }


    @Override
    public Message<List<Send>> selectBySendUserIdAndSemId(String sendUserId,Integer semId){
        logger.info("提交-----通过UserId查询,userId为 "+sendUserId);
        List<Send> a = sendMapper.selectBySendUserIdAndSemId1(sendUserId,semId);
        System.out.println(a);
        for (Send s:a) {
            Review a1 = s.getReview();
            String a2 = s.getSendId();
            if(a1==null){
                Review a3 = reviewMapper.selectBySendId(a2);
                System.out.println(a3);
                if(a3==null){
                    s.setReview(new Review());
                }
                else
                    s.setReview(a3);
            }
        }
        return Message.success(null).add(a);
    }


    @Override
    public Message<List<Send>> getSendByManyList(String flistId, String slistId, String tlistId,String sendSemseterId) {
        if (flistId.equals("")&&flistId != null){
            flistId = null;
        }
        if (slistId.equals("")&&slistId != null){
            slistId = null;
        }
        if (tlistId.equals("")&&tlistId != null){
            tlistId = null;
        }
        if (sendSemseterId.equals("")&&sendSemseterId !=null){
            sendSemseterId = null;
        }
        logger.info("提交-----通过路由得到提交");
        return Message.success(null).add(sendMapper.getSendByManyList(flistId,slistId,tlistId,sendSemseterId));
    }


    @Override
    public Message<List<Send>> getSendByTwoList(String flist, String slist) {
        logger.info("提交-----通过两个路由得到提交");
        return Message.success(null).add(sendMapper.getSendByManyList(flist,slist,null,null));
    }


    @Override
    public Message<Map<String,Float>> getScoreByUserId(String userId) {
            Map<String,Float> scoremap  =  new HashMap<>();
            List<Send> sends = sendMapper.selectBySendUserIdOrdered(userId);
            float sumscore =0;
            for (Send s:sends) {
                if (s.getSendScore()!=null) {
                    sumscore += s.getSendScore();
                }
            }
            scoremap.put(userId,sumscore);
            logger.info("提交-----得到userId "+userId+" 的总分为 "+sumscore);
            return Message.success("查询 "+userId+" 得分为 ").add(scoremap);
    }


    @Override
    public Message<List<Send>> getAllAvScore() {
        logger.info("提交-----得到所有人的平均分");
        return Message.success("操作成功,前端的伙伴们请注意!!!!!!!!!!!!sendScore为平均分").add(sendMapper.getAllAvScore());
    }


    @Override
    public Message<List<Send>> getAllPR() {
        logger.info("提交-----得到所有人的及格率");
        return Message.success("操作成功,前端的伙伴们请注意!!!!!!!!!!!!sendScore为及格率").add(sendMapper.getAllPR());
    }

    @Override
    public Message<List<Send>> selectAllAvScore() {
        logger.info("提交-----得到党支部的平均分");
        return Message.success("操作成功,前段的伙伴们请注意!!!!!!!!!!!!sendScore为平均分").add(sendMapper.selectAllAvScore());
    }

    @Override
    public Message<List<Send>> selectAllPR() {
        logger.info("提交-----得到党支部的及格率");
        return Message.success("操作成功,前段的伙伴们请注意!!!!!!!!!!!!sendScore为及格率").add(sendMapper.selectAllPR());
    }

    @Override
    public Message<List<Send>> getsendsBySemesterId(Integer semesterId) {
        return Message.success(null).add(sendMapper.getSendsBySemesterId(semesterId));
    }

    @Override
    public Message<?> getRouterAndPublicInfo(String flistId, String slistId, String tlistId, String sendSemseterId) {
        String nolook = "nolook";

        HashMap map = new HashMap<String,Object>();
        if(tlistId.equals(nolook)){
            if(slistId.equals(nolook)){
                Flist  flist= flistMapper.selectByPrimaryKey(flistId);
                List<UserInfoBranch> sends = userInfoService.showSend(flistId,slistId,tlistId,sendSemseterId);
                for (UserInfoBranch userInfoBranch:sends){

                    if(userInfoBranch.getSend() == null){
                        userInfoBranch.setSend(new Send());
                        userInfoBranch.getSend().setReview(new Review());
                    }
                    if (userInfoBranch.getSend()!=null&&userInfoBranch.getSend().getSendId()!=null){
                        if (reviewMapper.selectBySendId(userInfoBranch.getSend().getSendId())!=null){
                            userInfoBranch.getSend().setReview(reviewMapper.selectBySendId(userInfoBranch.getSend().getSendId()));
                        }else{
                            userInfoBranch.getSend().setReview(new Review());
                        }
                    }


                }
                map.put("router",flist);
                map.put("send",sends);
                return Message.success(null).add(map);
            }else {
                Slist slist = slistMapper.selectByPrimaryKey(slistId);
                List<UserInfoBranch> sends = userInfoService.showSend(flistId,slistId,tlistId,sendSemseterId);

                for (UserInfoBranch userInfoBranch:sends){

                    if(userInfoBranch.getSend() == null){
                        userInfoBranch.setSend(new Send());
                        userInfoBranch.getSend().setReview(new Review());
                    }
                    if (userInfoBranch.getSend()!=null&&userInfoBranch.getSend().getSendId()!=null){
                        if (reviewMapper.selectBySendId(userInfoBranch.getSend().getSendId())!=null){
                            userInfoBranch.getSend().setReview(reviewMapper.selectBySendId(userInfoBranch.getSend().getSendId()));
                        }else{
                            userInfoBranch.getSend().setReview(new Review());
                        }
                    }
                }
                map.put("router",slist);
                map.put("send",sends);
                return Message.success(null).add(map);
            }
        }else {
            Tlist tlist = tlistMapper.selectByPrimaryKey(tlistId);
            List<UserInfoBranch> sends = userInfoService.showSend(flistId,slistId,tlistId,sendSemseterId);
            for (UserInfoBranch userInfoBranch:sends){

                if(userInfoBranch.getSend() == null){
                    userInfoBranch.setSend(new Send());
                    userInfoBranch.getSend().setReview(new Review());
                }
                if (userInfoBranch.getSend()!=null&&userInfoBranch.getSend().getSendId()!=null){
                    if (reviewMapper.selectBySendId(userInfoBranch.getSend().getSendId())!=null){
                        userInfoBranch.getSend().setReview(reviewMapper.selectBySendId(userInfoBranch.getSend().getSendId()));
                    }else{
                        userInfoBranch.getSend().setReview(new Review());
                    }
                }

            }
            map.put("router",tlist);
            map.put("send",sends);
            return Message.success(null).add(map);
        }
    }

    @Override
    public Message<?> getRouterAndPublicInfoName(String flistId, String slistId, String tlistId, String sendSemseterId, String likeName) {
        String nolook = "nolook";


        HashMap map = new HashMap<String,Object>();
        if(tlistId.equals(nolook)){
            if(slistId.equals(nolook)){
                Flist  flist= flistMapper.selectByPrimaryKey(flistId);
                List<UserInfoBranch> sends = userInfoService.showSendLikeName(flistId,slistId,tlistId,sendSemseterId,likeName);
                for (UserInfoBranch userInfoBranch:sends){
                    if(userInfoBranch.getSend() == null){
                        userInfoBranch.setSend(new Send());
                        userInfoBranch.getSend().setReview(new Review());
                    }
                    if (userInfoBranch.getSend()!=null&&userInfoBranch.getSend().getSendId()!=null){
                        if (reviewMapper.selectBySendId(userInfoBranch.getSend().getSendId())!=null){
                            userInfoBranch.getSend().setReview(reviewMapper.selectBySendId(userInfoBranch.getSend().getSendId()));
                        }else{
                            userInfoBranch.getSend().setReview(new Review());
                        }
                    }
                }
                map.put("router",flist);
                map.put("send",sends);
                return Message.success(null).add(map);
            }else {
                Slist slist = slistMapper.selectByPrimaryKey(slistId);
                List<UserInfoBranch> sends = userInfoService.showSendLikeName(flistId,slistId,tlistId,sendSemseterId,likeName);
                for (UserInfoBranch userInfoBranch:sends){
                    if(userInfoBranch.getSend() == null){
                        userInfoBranch.setSend(new Send());
                        userInfoBranch.getSend().setReview(new Review());
                    }
                    if (userInfoBranch.getSend()!=null&&userInfoBranch.getSend().getSendId()!=null){
                        if (reviewMapper.selectBySendId(userInfoBranch.getSend().getSendId())!=null){
                            userInfoBranch.getSend().setReview(reviewMapper.selectBySendId(userInfoBranch.getSend().getSendId()));
                        }else{
                            userInfoBranch.getSend().setReview(new Review());
                        }
                    }
                }
                map.put("router",slist);
                map.put("send",sends);
                return Message.success(null).add(map);
            }
        }else {
            Tlist tlist = tlistMapper.selectByPrimaryKey(tlistId);
            List<UserInfoBranch> sends = userInfoService.showSendLikeName(flistId,slistId,tlistId,sendSemseterId,likeName);
            for (UserInfoBranch userInfoBranch:sends){
                if(userInfoBranch.getSend() == null){
                    userInfoBranch.setSend(new Send());
                    userInfoBranch.getSend().setReview(new Review());
                }
                if (userInfoBranch.getSend()!=null&&userInfoBranch.getSend().getSendId()!=null){
                    if (reviewMapper.selectBySendId(userInfoBranch.getSend().getSendId())!=null){
                        userInfoBranch.getSend().setReview(reviewMapper.selectBySendId(userInfoBranch.getSend().getSendId()));
                    }else{
                        userInfoBranch.getSend().setReview(new Review());
                    }
                }
            }
            map.put("router",tlist);
            map.put("send",sends);
            return Message.success(null).add(map);
        }
    }


    @Override
    public Message<?> pushSend(SendList2 sendList2) {
            Send send1 = sendMapper.getSendByManyListAndSemseterIdAndUserId(sendList2.getFlistId(), sendList2.getSlistId(), sendList2.getTlistId(), sendList2.getSendSemseterId(), sendList2.getUserId());
            if (send1 != null) {
                send1.setSendStatus(1);
                send1.setSendScore(sendList2.getSendScore());
                send1.setSendUpdateTime(new Date());
                return sendMapper.updateByPrimaryKeySelective(send1) > 0 ? Message.success(null) : Message.fail(null);
            } else {
                Send send2 = new Send();
                String sendId = "SE" + SendUtil.genSendId();
                send2.setSendId(sendId);
                send2.setSendUserId(sendList2.getUserId());
                send2.setSendFlistId(sendList2.getFlistId());
                send2.setSendSlistId(sendList2.getSlistId());
                send2.setSendTlistId(sendList2.getTlistId());
                send2.setSendSemseterId(sendList2.getSendSemseterId());
                send2.setSendGiveUserId(sendList2.getUserName());
                send2.setSendScore(sendList2.getSendScore());
                send2.setSendStatus(1);
                send2.setSendCreateTime(new Date());
                send2.setSendUpdateTime(new Date());
                return sendMapper.insertSelective(send2) > 0 ? Message.success(null) : Message.fail(null);
            }
    }

    @Override
    public Message<?> setSendStatusAndSendScoreIsNull(String sendId, Integer sendStatus, String giveUserId) {
        return sendMapper.setSendStatusAndSendScoreIsNull(sendId,sendStatus,giveUserId)> 0 ? Message.success(null) : Message.fail(null);
    }


}
